/*
 *  this file store the relate quetions of all the questions
 *
 */
#include "header.h"
RelatedQuestions::RelatedQuestions()
{
}

RelatedQuestions::RelatedQuestions(const char* relatedFileName, const char * inlinkFile,
                int neighbor_num)
{
    this->neighbor_num = neighbor_num;
    //load inlinkFile
    loadInLink(inlinkFile);
    //load related questoins
    num = loadRelatedQuestions(relatedFileName);

}
void RelatedQuestions::load(const char* relatedFileName, const char * inlinkFile,
                int num, int neighbor_num)
{
    this->neighbor_num = neighbor_num;
    this->num = num+1;
    this->inlinks.resize(this->num);
    this->relates.resize(this->num);
    //load inlinkFile
    loadInLink(inlinkFile);
    //load related questoins
    num = loadRelatedQuestions(relatedFileName);

}

RelatedQuestions::~RelatedQuestions()
{
    inlinks.clear();
    relates.clear();
}

int RelatedQuestions::getInlink(int qId)
{
    return this->inlinks[qId];
}

void RelatedQuestions::updateRelatedQuestions(int qId, vector<mypair> &knns)
{
    //minus all the inlink num of original neighbors
    vector<int>::iterator endInt = relates[qId].end(); 
    for (vector<int>::iterator iter = relates[qId].begin();
            iter != endInt; ++iter) {
        if (inlinks[*iter] > 0) --inlinks[*iter];
        else inlinks[*iter] = 0;
//        cout << *iter << " "<< inlinks[*iter] << endl;
    }
    //clear all the original relates
    relates[qId].clear();
    vector<mypair>::iterator endPair = knns.end();
    //add the new neighbors to the links and update the inlink num
    for (vector<mypair>::iterator iter = knns.begin();
            iter != endPair; ++iter) {
        relates[qId].push_back(iter->first);
        ++inlinks[iter->first];
//        cout << iter->first << " " << inlinks[iter->first] << endl;
    }
}

int RelatedQuestions::loadInLink(const char* inLinkFile)
{
    return loadIntVector(inLinkFile, inlinks);
}

int RelatedQuestions::loadRelatedQuestions(const char* relatedFileName)
{
    return loadIntVectorArray(relatedFileName, this->relates, this->neighbor_num);
}
